<?php
	session_start();
	if(isset($_POST['quantity'])) {
		//print_r($_POST['quantity']);
		foreach($_POST['quantity'] as $key => $el) {
			$_SESSION['cart'][$key][1] = $el;
		}
	}
	include_once('misc.php');
	//$books = get_books();
	
	$books = array();
	$total = 0.0;
	if(isset($_SESSION['cart'])) {
		foreach($_SESSION['cart'] as $id) {
			$book = get_book($id[0]);
			$book['quantity'] = $id[1];
			$books[] = $book;
			$total += $id[1]*$books[count($books) - 1]['price'];
		}
	}
?>
<!DOCTYPE html> <!-- HTML 5.0 -->
<html>
	<head>
		<meta http-equiv="Content-Type" 
					content="text/html; charset=utf-8"/>
		<title>Книжная лавка</title>
		<link rel="stylesheet" type="text/css" href="style.css"/>
	</head>
	<body>
		<div class="container">
		<!-- Header -->
		<div class="header">
			<img class="logo" width="200" height="200" alt="logo" 
						src="img/logo.png"/>
			<ul>
				<li><a href="cart.php">Корзина</a></li>
				<?php if($_SESSION['login']) { ?>
				<li><a href="logout.php">Выйти</a></li>
				<? } else { ?>
				<li><a href="reg.php">Регистрация</a></li>
				<form action="login.php" method="POST">
					<input type="textfield" name="login"/>
					<input type="password" name="pass"/>
					<input type="submit" value="Войти"/>
				</form>
				<? } ?>
			</ul>
			<div class="clear"></div>
		</div>
		<!-- Список книг -->
		<div class="main">
			<form action="cart.php" method="POST">
			<?php foreach($books as $i => $book) { ?>
				<div class="book">
					<img class="book_img" width="100" height="100" 
						alt="<?=iconv("cp866", "utf-8", $book['title'])?>" 
						src="img/books/<?=$book['id']?>.png"/>
					<h2><?=iconv("cp866", "utf-8", $book['title'])?></h2>
					цена:<span class="price"><?=$book['price']?></span>
					Кол-во: <input type="textfield" 
								value="<?=$book['quantity']?>"
								name="quantity[<?=$i?>]"/>
					<div class="clear"></div>
				</div>
				
			<?	  } ?>
				<input type="submit" value="Пересчитать"/>
			</form>
			<div class="right">
				<span class="total">ИТОГО:<?=$total?></span>
				<form action="make_order.php" method="POST">
					<input type="submit" value="Заказать"/>
				</form>
			</div>
			
		</div>
		<!-- footer -->
		<div class="footer">
			CC-BY PM 2012
		</div>
		</div>
	</body>
</html>